/**
 * File    : RequestUtils.java
 * Created : 2008-10-18
 * By      : LuHuiguo
 */
package com.luhuiguo.sso.web.util;

import javax.servlet.http.HttpServletRequest;

/**
 * @author LuHuiguo
 * @version $Id: RequestUtils.java 19 2008-10-18 15:59:28Z LuHuiguo $
 * 
 */
public class RequestUtils {
	
	public static String lookupDomainName(HttpServletRequest request) {
		if (request == null)
			return null;
		String port = "";
		if (!isStandardPort(request.getScheme(), request.getServerPort()))
			port = (new StringBuilder()).append(":").append(
					request.getServerPort()).toString();
		return (new StringBuilder()).append(request.getScheme()).append("://")
				.append(request.getServerName()).append(port).append(
						request.getContextPath()).append("/").toString();
	}

	private static boolean isStandardPort(String scheme, int port) {
		if (scheme.equalsIgnoreCase("http") && port == 80)
			return true;
		return scheme.equalsIgnoreCase("https") && port == 443;
	}
}
